var t = getApp(),
  a = t.requirejs("core");
Page({
  data: {
    icons: t.requirejs("icons"),
    status: "",
    list: [],
    mtype: 1,
    page: 1,
    cancel: ["我不想买了", "信息填写错误，重新拍", "其他原因"], //订单取消理由
    cancelindex: 0,
    alert_color: t.config.alert_color,
    button_color: t.config.button_color,
  },
  onLoad: function(e) {
    a.setting();
    "" == t.getCache("userinfo") && wx.redirectTo({
        url: "/pages/error/index"
      }),
      console.log("param",e)
    this.setData({
        options: e,
        status: e.status || "",
        mtype: e.mtype || 1,
        alert_color: getApp().config.alert_color,
        button_color: getApp().config.button_color
    }),

    this.get_list()
  },

  onShow: function () {
    this.setData({
      list: [],
      page: 1
    })
    this.get_list()
  },

  /**
   *获取订单列表
   * @param status uid page
   * @return array
   */
  get_list: function() {
    var t = this;
    if (t.data.mtype == 2) {
      a.ReName('我的秒杀订单');
    } else {
      a.ReName('我的砍价订单');
    }
    t.setData({
        loading: true,
      }),
      a.get("Bargain/OrderList", {
        page: t.data.page,
        status: t.data.status,
        mtype: t.data.mtype,
        uid: getApp().getCache("userinfo").uid
      }, function(e) {

        t.setData({
          list: [],
          total: 0,
        });
        0 == e.code ? (t.setData({
            loading: false,
            show: true,
            total: e.info.length,
            empty: false
          }),

          e.info.length > 0 && t.setData({
            page: t.data.page + 1,
            list: t.data.list.concat(e.info)
          }),
          e.info.length < 10 && t.setData({
            loaded: true
          })) : a.alert(e.msg)
      }, this.data.show)
  },

  /**
   *不同订单状态之间切换
   */
  selected: function(t) {
    var e = a.data(t).type;
    this.setData({
        list: [],
        page: 1,
        status: e,
        empty: true
      }),
      this.get_list()
  },


  //下拉刷新
  onPullDownRefresh: function() {
    this.setData({
        list: [],
        page: 1
      }),
      this.get_list()
    wx.stopPullDownRefresh();
  },
  //上拉加载更多
  onReachBottom: function() {
    this.data.loaded || this.get_list()
  },

  /**
   *取消订单
   * @param order_id buyer_id cancel_text
   * @return string
   */
  cancel: function(t) {
    var that = this,
      cancel_text = this.data.cancel[t.detail.value],
      s = a.data(t).orderid;
    a.get("Bargain/CancelOrder", {
      order_id: s,
      cancel_text: cancel_text,
      buyer_id: getApp().getCache("userinfo").uid
    }, function(e) {
      0 == e.code ? (
        that.setData({
          page: 1,
          list: []
        }),
        that.get_list(),
        a.alert('取消订单成功！')
      ) : a.alert(e.msg)
    })

  },

  /**
   *删除订单
   * @param order_id buyer_id 
   * @return string
   */
  delete: function(t) {
    var that = this,
      i = a.data(t).orderid;
    a.get("Bargain/DelOrder", {
      order_id: i,
      buyer_id: getApp().getCache("userinfo").uid
    }, function(e) {
      0 == e.code ? (
        that.setData({
          page: 1,
          list: []
        }), that.get_list(),
        a.alert('删除订单成功！')

      ) : a.alert(e.msg)
    }, )

  },
  /**
   *退款
   * @param order_id 
   * @return string
   */
  refund: function(i) {
    var that = this,
        order_id = a.data(i).orderid;
    a.jump('/yb_mingpian/pages/refund/apply?type=2&id=' + order_id);
  },

  // 退款查询
  to_refund: function (t) {
    var id = a.pdata(t).id;
    a.jump('/yb_mingpian/pages/refund/list?type=2&id=' + id);
  },

  /**
   *确认收货
   * @param order_id buyer_id 
   * @return string
   */
  finish: function(t) {
    var that = this,
      s = a.data(t).orderid;
    a.get("Bargain/SignOrder", {
      order_id: s,
      buyer_id: getApp().getCache("userinfo").uid
    }, function(e) {
      0 == e.code ? (
        that.setData({
          page: 1,
          list: []
        }), that.get_list(),
        a.alert('使用成功！')
      ) : a.alert(e.msg)
    })
  },

  // 核销码
  to_barcode: function (t) {
    var code = a.pdata(t).code + 'k';
    a.jump('/yb_mingpian/pages/barcode/index?code=' + code);
  },

})